<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f0f2f5; /* 改为柔和的灰色背景 */
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
            padding: 30px;
            max-width: 400px;
            width: 100%;
            margin: 20px;
            position: relative;
        }

        h1 {
            margin-top: 0;
            color: #333;
            font-size: 24px;
            text-align: center;
            margin-bottom: 20px;
        }

        input[type="text"],
        input[type="password"],
        input[type="email"] {
            width: 100%;
            padding: 12px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-sizing: border-box;
            font-size: 16px;
        }

        button {
            background-color: #4CAF50; /* 使用清新的绿色作为按钮背景色 */
            color: white;
            border: none;
            padding: 12px 20px;
            margin: 10px 0;
            border-radius: 5px;
            cursor: pointer;
            font-size: 18px;
            width: 100%;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #45a049; /* 悬停时使用稍深的绿色 */
        }

        .section {
            margin-bottom: 20px;
        }

        .timer-container {
            display: flex;
            align-items: center;
            margin-top: 10px;
            justify-content: space-between;
        }

        #timer {
            font-size: 16px;
            color: #555;
            background-color: #f4f4f9;
            padding: 5px 10px;
            border-radius: 4px;
            border: 1px solid #ddd;
            flex-grow: 1;
            text-align: center;
        }

        .back-link {
            position: absolute;
            bottom: 10px;
            right: 10px;
            font-size: 14px;
            color: #4CAF50;
            text-decoration: none;
        }

        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="section">
            <h1>请注册</h1>
            <input type="text" id="username" placeholder="用户名" required>
            <input type="password" id="password" placeholder="密码" required>
            <input type="password" id="confirmPassword" placeholder="确认密码" required>
            <input type="email" id="email" placeholder="邮箱" required>
            <div class="timer-container">
                <button onclick="sendVerification()">发送验证码</button>
                <p id="timer"></p>
            </div>
        </div>

        <div class="section">
            <h1>请输入验证码</h1>
            <input type="text" id="verificationCode" placeholder="验证码" required>
            <button onclick="registerUser()">注册</button>
        </div>

        <a href="login.html" class="back-link">已有账号？去登录</a>
    </div>

    <script>
        let timerInterval;
        let elapsedTime = 0;

        function startTimer() {
            if (timerInterval) {
                clearInterval(timerInterval);
            }
            elapsedTime = 0;
            timerInterval = setInterval(() => {
                elapsedTime++;
                document.getElementById('timer').textContent = `距离发送验证码已过: ${elapsedTime} s`;
            }, 1000);
        }

        function resetTimer() {
            clearInterval(timerInterval);
            elapsedTime = 0;
            document.getElementById('timer').textContent = '';
        }

        function sendVerification() {
            const email = document.getElementById('email').value;
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            if (password !== confirmPassword) {
                alert('两次输入的密码不一致，请重新输入');
                return;
            }

            if (!email || !username || !password) {
                alert('请填写所有必填字段');
                return;
            }

            fetch('/register_test', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ email, username, password })
            })
            .then(response => response.text())
            .then(text => {
                alert(text);
                if (text.includes('注册成功，请检查您的电子邮件，以验证您的帐户')) {
                    resetTimer();
                    startTimer();
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('发送验证码时发生错误');
            });
        }

        function registerUser() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const email = document.getElementById('email').value;
            const verificationCode = document.getElementById('verificationCode').value;

            if (!verificationCode) {
                alert('请输入验证码');
                return;
            }

            fetch('/verify', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password, email, verificationCode })
            })
            .then(response => response.text())
            .then(text => {
                if (text.includes('您的帐户已成功验证，会话数据已更新')) {
                    window.location.href = 'login.html';
                } else {
                    alert(text);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('注册时发生错误');
            });
        }
    </script>
</body>
</html>